/**
 * 
 */
package wong.board.client.ui;

import java.util.ArrayList;
import java.util.Set;

import wong.board.client.ui.NotesPresenter.View;
import wong.board.client.widgets.ContentContainer;
import wong.board.client.widgets.Table;
import wong.board.shared.ContentKeys;

import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Label;

/**
 * @author wong
 * 
 */
public class NotesView extends ContentContainer implements View {

	public NotesView() {
		super(ContentKeys.Notes.toString());
		addStyleName(boardCss.notes());
	}

	@Override
	public void removeNotes() {
		getContent().clear();
	}

	@Override
	public void addNote(String label, JSONObject note) {
		Table table = new Table();
		table.addStyleName(boardCss.noteTable());
		Set<String> keySet = note.keySet();
		for (String key : keySet) {
			String value = note.get(key).isString().stringValue();
			table.addRowData(createDataRow(key, value)).asWidget().addStyleName(boardCss.noteRow());
		}
		ContentContainer container = new ContentContainer(label);
		container.addStyleName(boardCss.note());
		container.addLabelStyleName(boardCss.noteLabel());
		container.getContent().add(table);
		getContent().add(container);
	}

	protected IsWidget[] createDataRow(String title, String text) {
		ArrayList<IsWidget> rowDataList = new ArrayList<IsWidget>();
		Label titleLabel = new Label(title);
		titleLabel.setStyleName(boardCss.noteCell());
		titleLabel.addStyleName(boardCss.noteCellTitle());
		rowDataList.add(titleLabel);
		Label textLabel = new Label(text);
		textLabel.setStyleName(boardCss.noteCell());
		textLabel.addStyleName(boardCss.noteCellText());
		rowDataList.add(textLabel);
		return rowDataList.toArray(new IsWidget[rowDataList.size()]);
	}
}
